package com.frank.ycj520.customview.commonview.loadingview.indicators;

import android.animation.ValueAnimator;
import android.view.animation.LinearInterpolator;
import java.util.ArrayList;

/**
 * Created by GJSON on 2015/10/19.
 */
public class BallZigZagDeflectIndicator extends BallZigZagIndicator {

  @Override public ArrayList<ValueAnimator> onCreateAnimators() {
    ArrayList<ValueAnimator> animators = new ArrayList<>();
    float startX = getWidth() / 6;
    float startY = getWidth() / 6;
    for (int i = 0; i < 2; i++) {
      final int index = i;
      ValueAnimator translateXAnim =
          ValueAnimator.ofFloat(startX, getWidth() - startX, startX, getWidth() - startX, startX);
      if (i == 1) {
        translateXAnim =
            ValueAnimator.ofFloat(getWidth() - startX, startX, getWidth() - startX, startX,
                getWidth() - startX);
      }
      ValueAnimator translateYAnim =
          ValueAnimator.ofFloat(startY, startY, getHeight() - startY, getHeight() - startY, startY);
      if (i == 1) {
        translateYAnim =
            ValueAnimator.ofFloat(getHeight() - startY, getHeight() - startY, startY, startY,
                getHeight() - startY);
      }

      translateXAnim.setDuration(2000);
      translateXAnim.setInterpolator(new LinearInterpolator());
      translateXAnim.setRepeatCount(-1);
      addUpdateListener(translateXAnim, new ValueAnimator.AnimatorUpdateListener() {
        @Override public void onAnimationUpdate(ValueAnimator animation) {
          translateX[index] = (float) animation.getAnimatedValue();
          postInvalidate();
        }
      });

      translateYAnim.setDuration(2000);
      translateYAnim.setInterpolator(new LinearInterpolator());
      translateYAnim.setRepeatCount(-1);
      addUpdateListener(translateYAnim, new ValueAnimator.AnimatorUpdateListener() {
        @Override public void onAnimationUpdate(ValueAnimator animation) {
          translateY[index] = (float) animation.getAnimatedValue();
          postInvalidate();
        }
      });

      animators.add(translateXAnim);
      animators.add(translateYAnim);
    }
    return animators;
  }
}
